package com.blog.controller;

import com.blog.pojo.Article;
import com.blog.service.ArticleService;
import com.blog.service.impl.ArticleServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 分类查询文章列表
 */
@WebServlet("/SortServlet.do")
public class SortServlet extends HttpServlet {

    ArticleService articleService=new ArticleServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //存储的是分类名和分类文章集合的对应关系
        Map<String, List<Article>> map=new HashMap<String, List<Article>>();
        Map<String, Integer> articleSortAndCount = articleService.getArticleSortAndCount();
        Set<Map.Entry<String, Integer>> entries = articleSortAndCount.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            String sort = entry.getKey();
            List<Article> articles = articleService.getArticlesBySort(sort);
            map.put(sort,articles);
        }
        req.setAttribute("sort_article_map",map);
        req.getRequestDispatcher("/page/sort.jsp").forward(req,resp);
    }
}
